import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		userInfo: {},
		// 当前的农场
		farm: uni.getStorageSync('farm') || {},
	},
	mutations: {
		login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',
			    data: provider
			})
			uni.setStorage({//缓存用户登陆状态
				key: 'hasLogin',
				data: true
			})
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({
                key: 'userInfo'
            })
			uni.setStorage({//缓存用户登陆状态
				key: 'hasLogin',
				data: false
			})
		},
		// 设置农场
		SET_FARM(state, farm) {
			state.farm = {
				...farm
			}
			uni.setStorageSync('farm', farm)
			console.log('uni.getStorageSync(\'farm\')',uni.getStorageSync('farm'))
		}
	},
	actions: {

	}
})

export default store
